package com.zcl.javaClassAndObject.collection.typeUnknown8_2_1;

import java.util.BitSet;
import java.util.Random;

/**
 * @Description:
 * @author: zcl
 * @date: 2021/12/13  11:47
 */


public class Bits {
    public static void main(String[] args) {
        Random rand = new Random();
        byte bt = (byte) rand.nextInt();
        BitSet bb = new BitSet();
        for (int i = 7; i >= 0; i--) {
            if (((1 << i) & bt) != 0) {
                bb.set(i);
            } else {
                bb.clear(i);
            }
        }
        System.out.println("byte value: " + bt);
        printBitSet(bb);
        short st = (short) rand.nextInt();
        BitSet bs = new BitSet();
        for (int i = 15; i >= 0; i--) {
            if (((1 << i) & st) != 0) {
                bs.set(i);
            } else {
                bs.clear(i);
            }
        }
        System.out.println("short value: " + st);
        printBitSet(bs);
        int it = rand.nextInt();
        BitSet bi = new BitSet();
        for (int i = 31; i >= 0; i--) {
            if (((1 << i) & it) != 0) {
                bi.set(i);
            } else {
                bi.clear(i);
            }
        }
        System.out.println("int value: " + it);
        printBitSet(bi);
        // Test bitsets >= 64 bits:
        BitSet b127 = new BitSet();
        b127.set(127);

        System.out.println("set bit 127: " + b127);
        BitSet b255 = new BitSet(65);
        b255.set(255);
        System.out.println("set bit 255: " + b255);
        BitSet b1023 = new BitSet(512);
        // Without the following, an exception is thrown
        // in the Java 1.0 implementation of BitSet:
        // b1023.set(1023);
        b1023.set(1024);
        System.out.println("set bit 1023: " + b1023);
    }

    static void printBitSet(BitSet b) {
        System.out.println("bits: " + b);
        String bbits = new String();
        for (int j = 0; j < b.size(); j++) {
            bbits += (b.get(j) ? "1" : "0");
        }
        System.out.println("bit pattern: " + bbits);
    }
}
/**
 * byte value: -47
 * bits: {0, 4, 6, 7}
 * bit pattern: 1000101100000000000000000000000000000000000000000000000000000000
 * short value: 5361
 * bits: {0, 4, 5, 6, 7, 10, 12}
 * bit pattern: 1000111100101000000000000000000000000000000000000000000000000000
 * int value: 1511829698
 * bits: {1, 6, 7, 12, 13, 15, 18, 19, 20, 25, 27, 28, 30}
 * bit pattern: 0100001100001101001110000101101000000000000000000000000000000000
 * set bit 127: {127}
 * set bit 255: {255}
 * set bit 1023: {1024}
 * */